package org.pictual.controller.mainPackage;

import java.io.BufferedReader;
import java.util.Vector;

public abstract class WikiDicParser extends MyParser {

    protected final static String pTag = "<p><a name=\"";
    protected Vector<String> _otherLans;
    CategoriesWrapper _categories;

    abstract protected boolean IsType(String type);
    abstract public String WikiLang(int lang);
    abstract protected boolean isLang(String language);
    abstract protected boolean extractDefinitionList(BufferedReader input, DefinitionList listdef, int lang);

    DefinitionList list=new DefinitionList();
    public String query=null;

    public WikiDicParser() {
    }

    public DefinitionList getDefList() {
    	return list;
    }

    public Vector<String> GetOtherLanguages() {
        return _otherLans;
    }
    public CategoriesWrapper GetCategoriesWrapper() {
        return _categories;
    }

    public static String resolveName(String name) {
        int index = name.indexOf('_');
        String rname = name;
        if (index != -1) {
            rname = name.substring(0, index);
        }
        return rname;
    }

    public boolean Parse(BufferedReader input, int lang) {
        /*
        <p><a name="English" id="English"></a></p>
        <p><a name="Etymology" id="Etymology"></a></p>
        <p><a name="Pronunciation" id="Pronunciation"></a></p>
        <p><a name="Noun" id="Noun"></a></p>
        <ol>
        </ol>
        <p><a name="Synonyms" id="Synonyms"></a></p>
        <p><a name="Derived_terms" id="Derived_terms"></a></p>
        <p><a name="Related_terms" id="Related_terms"></a></p>
        <p><a name="Translations" id="Translations"></a></p>
        <p><a name="External_links" id="External_links"></a></p>
        <p><a name="Verb" id="Verb"></a></p>
        <ol>
        </ol>
         */

        _otherLans = new Vector<String>();
        _categories = new CategoriesWrapper();

        boolean insideLan = false;
        int hIndex = 0;

        System.out.println("WikiDicParser-Parsing");
        String rawLine;
        try {
            //while ((rawLine = input.readLine()) != null) {
            	//System.out.println("[_"+rawLine+"_]");
            	//if (rawLine.equals("<li></li>"))
            		//System.out.println("found:"+rawLine);
                //String line = rawLine.replaceAll("\t", "");
                //if (line.startsWith(pTag)) {
                    //String nextLine = input.readLine();
//                    String name = "English";//query;//extractField(line, pTag.length());
//                    if (_otherLans.isEmpty() && name.equals(WikiLang(lang))) {
//                        insideLan = true;
//                        String nextline = input.readLine();
//                        hIndex = Integer.parseInt(nextline.substring(2, 3));
//                    } else if (!insideLan && isLang(name)) {
//                        _otherLans.addElement(name);
//                    } else if (insideLan) {
//                        String nextline = input.readLine();
//                        int index = Integer.parseInt(nextline.substring(2, 3));
//                        if (index == hIndex) {
//                            return;
//                        } else if (IsType(name)) {
//                            name = resolveName(name);
//                            System.out.println("WikiDikParser:name:"+name);
                            list = _categories.getDefinitionList("English");
                            System.out.println("[WikiDikParser:getDL:"+_categories.getDefinitionList("English"));
                            boolean extract = extractDefinitionList(input, list, lang);
                            System.out.println("parse: "+extract);
                            return extract;
//                        }
//                    }
                // }
           // }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return true;
    }
}
